//
//  STProductDetailController.m
//  CuiBoStore
//
//  Created by 崔博 on 16/12/8.
//  Copyright © 2016年 崔博. All rights reserved.
//

#import "STProductDetailController.h"
#import "STDeitailsTopImageView.h"
#import "STProductHandle.h"
#import "STProductDeitailLabelView.h"
#import "STProductContendViw.h"
#import "STProductDeitailBottomView.h"
#import "STProductTabView.h"
#import "STBuyCarHandle.h"
@interface STProductDetailController ()
@property (nonatomic, strong) UIScrollView *backScro;
@property (nonatomic, strong) STDeitailsTopImageView *topView;
@property (nonatomic, strong) STProductDeitailLabelView *detailLabelView;
@property (nonatomic, strong) STProductContendViw *contenView;
@property (nonatomic, strong) STProductDeitailBottomView *bottomView;
@property (nonatomic, strong) STProductTabView *tabbarView;
@end

@implementation STProductDetailController

#pragma mark - life
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor redColor];
    // 加载控件
    [self setUpUI];
    // 请求数据
    [self loadData];
}

#pragma mark - methoed
// 加载控件
- (void)setUpUI
{
    
    [self.view addSubview:self.backScro];
    [self.view addSubview:self.tabbarView];
    
    [_backScro mas_makeConstraints:^(MASConstraintMaker *make) {
        MASONRYINSERT(self.view, 0, 0, 45, 0);
    }];
    [self.backScro addSubview:self.topView];
    [self.backScro addSubview:self.detailLabelView];
    [self.backScro addSubview:self.contenView];
    [self.backScro addSubview:self.bottomView];
    
    [_detailLabelView mas_makeConstraints:^(MASConstraintMaker *make) {
        MASONRY(top, _topView.mas_bottom, 13);
        make.left.right.equalTo(self.view);
        make.height.equalTo(@300);
    }];
    [_contenView mas_makeConstraints:^(MASConstraintMaker *make) {
        MASONRY(top, _detailLabelView.mas_bottom, 0);
        make.right.left.equalTo(self.view);
        make.height.equalTo(@100);
    }];
    [_bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.left.equalTo(self.view);
        MASONRY(top, _contenView.mas_bottom, 0);
        make.height.equalTo(@100);
    }];
}
// 请求数据
- (void)loadData
{
    // 请求banner
    [STProductHandle excuteProducBannerGoodsId:self.detailsGoodsId sucess:^(id obj) {
        self.topView.imageUrlList = obj[KProductDetailBannerImageArray];
        self.bottomView.imageArray = obj[KProductDetailBottomImageArray];
    } failed:^(id obj) {
        
    }];
    
    // 请求标题等信息
    [STProductHandle excuteProductTitleDataGoodsId:self.detailsGoodsId sucess:^(id obj) {
        self.detailLabelView.titleModel = obj;
    } failed:^(id obj) {
    }];
    
    // 列表
    [STProductHandle excutProductList:self.detailsGoodsId sucess:^(id obj) {
        self.contenView.listArray = obj;
    } failed:^(id obj) {
        
    }];
}
// 加入购物车
- (void)addBuyCar
{
    [STBuyCarHandle excuteAddBuyCarGoodsId:self.detailsGoodsId sucess:^(id obj) {
       
        NSLog(@"%@",obj);
        
    } faile:^(id obj) {
        
    }];
}
#pragma mark - lazy
- (UIScrollView *)backScro
{
    if (!_backScro) {
        _backScro = [[UIScrollView alloc] init];
        _backScro.backgroundColor = BACKGROUND_COLOR;
    }
    return _backScro;
}

- (STDeitailsTopImageView *)topView
{
    if (!_topView) {
        _topView = [[STDeitailsTopImageView alloc] init];
        _topView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 380);
        _topView.backgroundColor = [UIColor greenColor];
    }
    return _topView;
}

- (STProductDeitailLabelView *)detailLabelView
{
    if (!_detailLabelView) {
        _detailLabelView = [[STProductDeitailLabelView alloc] init];
        @weakify(self);
        _detailLabelView.heghtBlock = ^(CGFloat h){
            [weak_self.detailLabelView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.equalTo(@(h));
            }];
        };
    }
    return _detailLabelView;
}

- (STProductContendViw *)contenView
{
    if (!_contenView) {
        _contenView = [[STProductContendViw alloc] init];
        @weakify(self)
        _contenView.block = ^(CGFloat h){
         [weak_self.contenView mas_updateConstraints:^(MASConstraintMaker *make) {
             make.height.equalTo(@(h));
         }];
        };
    }
    return _contenView;
}

- (STProductDeitailBottomView *)bottomView
{
    if (!_bottomView) {
        @weakify(self);
        _bottomView = [[STProductDeitailBottomView alloc] init];
        _bottomView.block = ^(CGFloat h){
          [weak_self.bottomView mas_updateConstraints:^(MASConstraintMaker *make) {
              make.height.equalTo(@(h));
          }];
            [weak_self.view layoutIfNeeded];
            weak_self.backScro.contentSize = CGSizeMake(0, weak_self.bottomView.maxY + 155);
        };
    }
    return _bottomView;
}
- (STProductTabView *)tabbarView
{
    if (!_tabbarView) {
        _tabbarView = [STProductTabView productTabbarView];
        @weakify(self)
        _tabbarView.block = ^(){
            // 加入购物车
            [weak_self addBuyCar];
        };
    }
    return _tabbarView;
}
@end
